<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form-model
        ref="formModel"
        :model="form"
        layout="inline"
        :rules="{
          name: [
            {
              validator: validators.noWhitespace(),
              trigger: 'change',
            },
            {
              validator: validators.noIllegalString(),
              trigger: 'change',
            },
            {
              max: 10,
              message: '长度在 1 到 10 个字符',
              trigger: 'change',
            },
          ],
        }"
      >
        <a-row :gutter="[16, 16]" type="flex" justify="end">
          <a-col :md="6" :sm="8">
            <a-form-model-item prop="name" label="交换机名称">
              <a-input autocomplete="off" v-model="form.name" />
            </a-form-model-item>
          </a-col>
          <span
            style="float: left; overflow: hidden"
            class="table-page-search-submitButtons"
          >
            <a-col :md="12" :sm="24">
              <a-button
                type="primary"
                @click="loadData(true)"
                icon="search"
                style="margin-left: 21px"
              >
                查询
              </a-button>
              <a-button
                icon="reload"
                style="margin-left: 8px"
                @click="resetSearchForm"
              >
                重置
              </a-button>
            </a-col>
          </span>
        </a-row>
      </a-form-model>
    </div>

    <a-table
      ref="table"
      rowKey="id"
      size="middle"
      :columns="columns"
      :dataSource="gDataSource"
      :pagination="gPagination"
      @change="gHandleTableChange"
    >
      <span slot="sortCol" slot-scope="text, record, index">
        {{
          (gPagination.current - 1) * gPagination.pageSize + parseInt(index) + 1
        }}
      </span>
    </a-table>
  </a-card>
</template>

<script>
import moment from "moment";
import { GwListMixin } from "@/mixins/GwListMixin";
import { gPostAction } from "@/api/manage";
import { sm3Hash } from "@/utils/sm3";
import { sm4Decrypt, randomString } from "@/utils/sm4"
import { SM4_KEY } from "@/store/mutation-types";

export default {
  name: "F010108",
  mixins: [GwListMixin],
  components: {},
  data() {
    return {
      // 表头
      columns: [
        {
          title: "序号",
          dataIndex: "",
          key: "rowIndex",
          width: 50,
          scopedSlots: { customRender: "sortCol" },
        },
        {
          title: "交换机名称",
          dataIndex: "name",
        },
        {
          title: "交换机IP",
          dataIndex: "ip",
        },
        {
          title: "交换机Community",
          ellipsis: true,
          dataIndex: "community",
        },
        {
          title: "数据中心",
          dataIndex: "datacenterName",
        },
        {
          title: "创建时间",
          dataIndex: "createTime",
          customRender: function (t, r, index) {
            return moment(t).format("YYYY-MM-DD HH:mm:ss");
          },
        },
      ],
      form: {
        name: "",
      },
      url: {
        list: "/asset/pageSwitcher",
      },
    };
  },
  mounted() {
    this.loadData();
  },
  methods: {
    loadData(isDoSearch) {
      this.$refs.formModel.validate((error) => {
        if (!error) {
          return;
        }
        // this.gQueryParam = {}
        this.gQueryParam.name = this.form.name;
        this.gQueryParam.title = "精确管控与供电制冷联动-基础资源细粒度感知-网络管理";
        this.gQueryParam.content = "查询网络设备列表";
        this.gQueryParam.order = "energy_efficiency_ratio";
        if (isDoSearch) {
          this.gQueryParam.pageNo = 1;
          this.gPagination.current = 1;
        }
        let sm4key = randomString();
        this.$ls.set(SM4_KEY, sm4key);
        gPostAction(this.url.list, this.gQueryParam).then((res) => {
          this.gDataSource = [];
          const data = res.data;
          if (data && data.list) {
            let list = []
            data.list.forEach((item) => {
              if (item.community) {
                const temp = item.community;
                let str = undefined;
                try {
                  str = sm4Decrypt(item.community, sm4key);
                } catch (error) {
                  str = undefined;
                }
                if (str) {
                  let len = str.length;
                  item.community = str.substring(0, len - 64) || temp;
                  let sm3Str = str.substring(len - 64, len);
                  if (
                    item.community &&
                    sm3Str &&
                    sm3Hash(item.community) !== sm3Str
                  ) {
                    // this.$message.warning('数据完整性被破坏,请注意数据安全。')
                  }
                }
              } else {
                // this.$message.warning('完整性被破坏,请注意数据安全。')
              }
              list.push(item);
            });
            this.gDataSource = list;
            this.gPagination.total = data.totalCount;
          }
        });
      });
    },
    resetSearchForm() {
      this.form.name = "";
      this.$refs.formModel.resetFields();
    },
  },
};
</script>
<style scoped>
</style>
